package com.baayso.demo.sj.common.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baayso.demo.sj.utils.SpringUtils;
import com.jfinal.ext.plugin.sqlinxml.SqlInXmlPlugin;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class StartListener implements ServletContextListener {

    private static final Logger logger = LoggerFactory.getLogger(StartListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("服务器启动……");

        DruidPlugin druidPlugin = SpringUtils.getBean(DruidPlugin.class);
        AutoTableBindPlugin autoTableBindPlugin = SpringUtils.getBean(AutoTableBindPlugin.class);
        SqlInXmlPlugin sqlInXmlPlugin = SpringUtils.getBean(SqlInXmlPlugin.class);

        druidPlugin.start();
        autoTableBindPlugin.start();
        sqlInXmlPlugin.start();

        logger.info("结束。");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        DruidPlugin druidPlugin = SpringUtils.getBean(DruidPlugin.class);
        AutoTableBindPlugin autoTableBindPlugin = SpringUtils.getBean(AutoTableBindPlugin.class);
        SqlInXmlPlugin sqlInXmlPlugin = SpringUtils.getBean(SqlInXmlPlugin.class);

        druidPlugin.stop();
        autoTableBindPlugin.stop();
        sqlInXmlPlugin.stop();
    }

}
